/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.widget.EllipsizingTextView;
import cn.com.dhc.danlu.widget.holder.DeliverySellerGoodsListHolder;
import cn.com.hd.domain.entity.entityext.GoodsExt;

/**
 * 配送中心（销售）ListView中商品列表用适配器
 *
 * @author MuWei
 * @version 1.00 2015/02/09 新建
 */
class DeliverySellerGoodsListAdapter extends BaseAdapter {
    /**
     * LayoutInflater。
     */
    private LayoutInflater layoutInflater;
    /**
     * 订单商品List。
     */
    private List<GoodsExt> collection;

    /**
     * 构造函数。
     *
     * @param context
     *         Context
     * @param collection
     *         订单商品List
     */
    DeliverySellerGoodsListAdapter(Context context, List<GoodsExt> collection) {
        // 初始化LayoutInflater
        layoutInflater = LayoutInflater.from(context);
        this.collection = collection;
    }

    @Override
    public int getCount() {
        return collection == null ? 0 : collection.size();
    }

    @Override
    public GoodsExt getItem(int position) {
        return collection.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        DeliverySellerGoodsListHolder holder;

        if (convertView == null) {
            // 实例化画面
            convertView = layoutInflater.inflate(R.layout.view_delivery_seller_goods_list_item, null);

            holder = new DeliverySellerGoodsListHolder();
            holder.imgGoods = (SimpleDraweeView) convertView.findViewById(R.id.imgGoods);
            holder.txtGoodsName = (EllipsizingTextView) convertView.findViewById(R.id.txtGoodsName);
            holder.txtGoodsBarCode = (TextView) convertView.findViewById(R.id.txtGoodsBarCode);
            holder.txtGoodsPrice = (TextView) convertView.findViewById(R.id.txtGoodsPrice);
            holder.txtGoodsCount = (TextView) convertView.findViewById(R.id.txtGoodsCount);
            convertView.setTag(holder);
        } else {
            holder = (DeliverySellerGoodsListHolder) convertView.getTag();
        }

        // 获取数据
        GoodsExt model = getItem(position);

        // 设置数据
        setData(holder, model);

        return convertView;
    }

    /**
     * 设置数据
     *
     * @param holder
     *         控件集合
     * @param model
     *         商品数据
     */
    private void setData(DeliverySellerGoodsListHolder holder, GoodsExt model) {
        // 最多显示两行
        holder.txtGoodsName.setMaxLines(2);
        // 设置商品名称
        holder.txtGoodsName.setText(model.getMerchandiseName());
        // 设置商品条码
        holder.txtGoodsBarCode.setText(model.getBarCode());
        // 设置商品价格
        holder.txtGoodsPrice.setText(String.valueOf(model.getUnitPrice()));
        // 设置商品数量
        holder.txtGoodsCount.setText(String.valueOf(model.getNum()));
        holder.imgGoods.setImageURI(model.getSaveUrl());
    }
}
